package org.mobiletrain.www.secondproject2;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import Utils.JsonUtil;
import bean.NearbysubData;


public class NearbySubFragment extends Fragment {


    private ArrayList<NearbysubData> nearbyList;

    public NearbySubFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_nearby_sub, container, false);
        Bundle arguments = getArguments();
        String path = arguments.getString("path");


        PullToRefreshListView  refreshListView=
                (PullToRefreshListView) view.findViewById(R.id.nearbysub_list);
        refreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });

        nearbyList = new ArrayList<>();
        JsonUtil.loadJson(getActivity(),path,new JsonUtil.OnJsonDown() {
            @Override
            public void ok(String json) {
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    JSONObject data = jsonObject.getJSONObject("data");
                    JSONArray items = data.getJSONArray("items");
                    for (int i = 0; i <items.length() ; i++) {
                        JSONObject object = items.getJSONObject(i);
                        String productId = object.getString("productId");
                        String productName = object.getString("productName");
                        String productDescription = object.getString("productDescription");
                        String productTitle = object.getString("productTitle");
                        String productTitleContent = object.getString("productTitleContent");
                        String mImageUrl = object.getString("mImageUrl");
                        String url600x360 = object.getString("url600x360");
                        String url = object.getString("url");
                        String retailPrice = object.getString("retailPrice");
                        String price = object.getString("price");
                        String originalPrice = object.getString("originalPrice");
                        String isNew = object.getString("isNew");
                        String flag = object.getString("flag");
                        String saledCount = object.getString("saledCount");
                        String provinceName = object.getString("provinceName");
                        String cityName = object.getString("cityName");
                        String scenicList = object.getString("scenicList");
                        String adress = object.getString("adress");
                        String coordinate = object.getString("coordinate");
                        String distance = object.getString("distance");
                        String goodRate = object.getString("goodRate");
                        String channelLinkId = object.getString("channelLinkId");


                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void error(String error) {
                Toast.makeText(getActivity(),error,Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }


}
